// StoperDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EventNotify.h"
#include "StoperDlg.h"
#include "afxdialogex.h"


// CStoperDlg dialog

IMPLEMENT_DYNAMIC(CStoperDlg, CDialogEx)

CStoperDlg::CStoperDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CStoperDlg::IDD, pParent)
	, m_ulElapsedTime(0)
	, m_ulStartTime(0)
	, m_strCounter(_T(""))
{

}

CStoperDlg::~CStoperDlg()
{
}

void CStoperDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_STATIC_COUNTER, m_strCounter);
	DDX_Control(pDX, IDC_STATIC_COUNTER, m_CounterCtrl);
}


BEGIN_MESSAGE_MAP(CStoperDlg, CDialogEx)
	ON_BN_CLICKED(IDC_START, &CStoperDlg::OnBnClickedStart)
	ON_BN_CLICKED(IDC_STOP, &CStoperDlg::OnBnClickedStop)
	ON_BN_CLICKED(IDC_RESET, &CStoperDlg::OnBnClickedReset)
	ON_BN_CLICKED(IDC_COPY, &CStoperDlg::OnBnClickedCopy)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CStoperDlg message handlers


void CStoperDlg::OnBnClickedStart()
{
	// TODO: Add your control notification handler code here
	m_ulStartTime = GetTickCount();
	SetTimer(ID_TIMER_STOPER, 10, NULL);
}


void CStoperDlg::OnBnClickedStop()
{
	// TODO: Add your control notification handler code here
	KillTimer(ID_TIMER_STOPER);
}


void CStoperDlg::OnBnClickedReset()
{
	// TODO: Add your control notification handler code here
	m_ulElapsedTime = 0;
	UpdateCounter();
}


void CStoperDlg::OnBnClickedCopy()
{
	// TODO: Add your control notification handler code here
	if(OpenClipboard())
	{
		HGLOBAL clipbuffer;
		char * buffer;
		EmptyClipboard();
		clipbuffer = GlobalAlloc(GMEM_DDESHARE, m_strCounter.GetLength()+1);
		buffer = (char*)GlobalLock(clipbuffer);
		wcstombs(buffer, LPCWSTR(m_strCounter), m_strCounter.GetLength());
		GlobalUnlock(clipbuffer);
		SetClipboardData(CF_TEXT,clipbuffer);
		CloseClipboard();
	}

}


void CStoperDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	unsigned long ulNow;
	if(nIDEvent == ID_TIMER_STOPER)
	{
		ulNow = GetTickCount();
		m_ulElapsedTime += ulNow - m_ulStartTime;
		m_ulStartTime = ulNow;
	}
	UpdateCounter();
	CDialogEx::OnTimer(nIDEvent);
}


void CStoperDlg::UpdateCounter(void)
{
	static unsigned long ulOneHour   = 1000 * 60 * 60;
	static unsigned long ulOneMinute = 1000 * 60;
	static unsigned long ulOneSecond = 1000;
	unsigned long ulHours, ulMinutes, ulSeconds, ulMilisec, ulTempTime;

	ulHours    = m_ulElapsedTime / ulOneHour;
	ulTempTime = m_ulElapsedTime % ulOneHour;
	ulMinutes  = ulTempTime / ulOneMinute;
	ulTempTime = ulTempTime % ulOneMinute;
	ulSeconds  = ulTempTime / ulOneSecond;
	ulTempTime = ulTempTime % ulOneSecond;
	ulMilisec  = ulTempTime / 10;

	if(ulHours < 10)
		m_strCounter.Format(_T("0%u:"), ulHours);
	else
		m_strCounter.Format(_T("%u:"), ulHours);

	if(ulMinutes < 10)
		m_strCounter.AppendFormat(_T("0%u:"), ulMinutes);
	else
		m_strCounter.AppendFormat(_T("%u:"), ulMinutes);

	if(ulSeconds < 10)
		m_strCounter.AppendFormat(_T("0%u."), ulSeconds);
	else
		m_strCounter.AppendFormat(_T("%u."), ulSeconds);

	if(ulMilisec < 10)
		m_strCounter.AppendFormat(_T("0%u"), ulMilisec);
	else
		m_strCounter.AppendFormat(_T("%u"), ulMilisec);

	UpdateData(FALSE);
}


BOOL CStoperDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  Add extra initialization here
	CFont *pfOldFont = m_CounterCtrl.GetFont();
	LOGFONT lfFont;
	
	pfOldFont->GetLogFont(&lfFont);
	lfFont.lfHeight = 45;
	m_fCounterFont.Detach();
	m_fCounterFont.CreateFontIndirect(&lfFont);
	m_CounterCtrl.SetFont(&m_fCounterFont);

	UpdateCounter();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CStoperDlg::OnFinalRelease()
{
	// TODO: Add your specialized code here and/or call the base class
	KillTimer(ID_TIMER_STOPER);
	CDialogEx::OnFinalRelease();
}
